#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int main(){
    int fd[2];
    int ret = pipe(fd);
    if(ret < 0){
        perror("pipe");
        return 0;
    }
    pid_t pid = fork();
    if(pid < 0){
        perror("fork");
        return 0;
    }
    if(pid == 0){
        //child
        //1、关闭读端
        //2、设置写端为非阻塞属性
        //3、写
        sleep(2);            //让父进程先运行，关闭发的fd[0]
        close(fd[0]);
        int flag = fcntl(fd[1], F_GETFL);
        fcntl(fd[1], F_SETFL, flag | O_NONBLOCK);
        ssize_t write_size;
        while(1){
            write_size = write(fd[1],"a",1);
            if(write_size < 0){
                break;
            }
        }
        printf("write_size = %ld\n",write_size);
        perror("write");
    }else{
        //father
        //1、关闭写端
        //2、读关闭/读不关闭
        //3、读
        close(fd[1]);
        close(fd[0]);
        while(1){
            sleep(1);
        }
    }
    return 0;
}
